<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\11\30 0030
 * Time: 10:47
 */

namespace core\lib;

class CController
{
    protected $assign = array();

    public function beforeAction(){
        return true;
    }

    protected function  assign($name, $value)
    {
        $this->assign[$name] = $value;
    }

    protected function display($name)
    {
        // 不使用模板引擎
        $viewPath = APP . '/' . MODULE . '/protected/views/' . Route::$controller . '/' . $name;
        extract($this->assign);
        if(is_file($viewPath)){
            include $viewPath;
        }else{
            throw new \Exception('找不到该视图文件' . $name);
        }

        // 使用了twig模板引擎
        /*$veiwPath = ROOT . MODULE . '/views/' . Route::$controller;
        $veiwFile = ROOT . MODULE . '/views/' . Route::$controller . '/' . $name;
        if(is_file($veiwFile)){
            $loader = new \Twig_Loader_Filesystem($veiwPath);
            $twig = new \Twig_Environment($loader, array(
                 'cache' => ROOT . '/cache/twig',
                'debug' => true
            ));
            echo $twig->render($name, $this->assign?:array());
        }else{
            throw new \Exception('找不到该视图文件' . $name);
        }*/
    }
}